package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.Bindable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TabHost;

import java.util.List;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.WaitActivityResult;

/**
 * Created by zxh on 16/11/17.
 */

public class HomeModel extends ContextObservable implements WaitActivityResult {
    private TabHost tabHost;
    private List<TabItem> tabItems;
    private boolean topBarVisible;

    @Bindable
    public boolean isTopBarVisible() {
        return topBarVisible;
    }

    public void setTopBarVisible(boolean topBarVisible) {
        this.topBarVisible = topBarVisible;
        notifyPropertyChanged(BR.topBarVisible);
    }

    public void setTabHost(TabHost tabHost){
        this.tabHost = tabHost;
    }

    public HomeModel(Context context, TabHost tabHost, List<TabItem> tabItems){
        super(context);
        this.tabHost = tabHost;
        this.tabItems = tabItems;
    }

    /**
     * 点击排行版
     * @param view
     */
    public void clickRank(View view){
        CommonFragment fragment = new CommonFragment();
        fragment.setLayoutId(R.layout.f_rank);
        fragment.setVariableId(BR.rankModel);
        fragment.setDataModel(new RankModel(getContext()));
        HomeActivity.switchFragment((Activity)getContext(), fragment);
    }

    /**
     * 点击首页搜索按钮
     * @param view
     */
    public void clickSearch(View view){
        CommonFragment fragment = new CommonFragment();
        fragment.setLayoutId(R.layout.f_search_result);
        fragment.setVariableId(BR.searchModel);
        fragment.setDataModel(new SearchModel(getContext()));
        HomeActivity.switchFragment((Activity)getContext(), fragment);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        TabItem item = tabItems.get(tabHost.getCurrentTab());
        Object model = item.getModel();
        if (model instanceof WaitActivityResult) {
            ((WaitActivityResult) model).onActivityResult(requestCode, resultCode, data);
        }
    }
}
